package com.ytjj.qmyx.supplychain.common.utils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class HttpParamUtil {
    /**
     * 获得url中参数
     * @param url
     * @return
     */
    public static Map getParameter(String url) {
        Map map = new HashMap();
        try {
            final String charset = "utf-8";
            url = URLDecoder.decode(url, charset);
            if (url.indexOf('?') != -1) {
                final String contents = url.substring(url.indexOf('?') + 1);
                String[] keyValues = contents.split("&");
                for (int i = 0; i < keyValues.length; i++) {
                    String key = keyValues[i].substring(0, keyValues[i].indexOf("="));
                    String value = keyValues[i].substring(keyValues[i].indexOf("=") + 1);
                    map.put(key, value);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;

    }

    /**
     * 获取request中参数
     *
     * @param request 页面请求
     */

    public static Map getRequestParameters(HttpServletRequest request) {
        String parameters = "";// 请求参数
        String urlParameter = request.getQueryString();// 网址中的参数
        if (urlParameter != null && !"".equals(urlParameter)) {
            try {
                urlParameter = URLDecoder.decode(urlParameter, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        } else {
            urlParameter = "";
        }
        parameters = urlParameter;
        Map map = new HashMap();
        String[] arr = parameters.split("&");
        for (int i = 0; i < arr.length; i++) {
            String key = arr[i].substring(0, arr[i].indexOf("="));
            String value = arr[i].substring(arr[i].indexOf("=") + 1);
            map.put(key, value);
        }
        return map;

    }

    /**
     * 测试
     *
     * @param args
     */

    public static void main(String[] args) {
        String url = "https://detail.1688.com/offer/642905492398.html?spm=a312h.2018_new_sem.dh_002.17.3e206035j9vVmD&tracelog=p4p&clickid=5298cc56e4604a4f8867595a20650531&sessionid=64f7c82bc11f0a050da067ab5b3cb055";

        Map map = getParameter(url);
        System.out.println(map);
        System.out.println(getDomain(url));
        //https://detail.tmall.com
        //https://www.jd.com  https://item.jd.com
        //https://item.taobao.com
        //https://detail.1688.com
    }

    /**
     * 获取网址域名
     * @param destination
     * @return
     */
    public static String getDomain(String destination) {
        if (destination == null || destination.trim().equals("")) {
            return "";
        }
        String domain = "";
        URL url = null;
        try {
            url = new URL(destination);
            domain = url.getProtocol() + "://" + url.getHost();
        } catch (MalformedURLException e) {
            System.out.println(e);
        }
        return domain;
    }

    /**
     * 获取网址 文件路径
     * @param destination
     * https://detail.1688.com/offer/642905492398.html?spm=a312h.2018_new_sem.dh_002.17.3e206035j9vVmD&tracelog=p4p&clickid=5298cc56e4604a4f8867595a20650531&sessionid=64f7c82bc11f0a050da067ab5b3cb055
     * 返回：offer/642905492398.html
     * @return
     */
    public static String getUrlPath(String destination) {
        URL url = null;
        try {
            url = new URL(destination);
            return url.getPath();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

}